package com.emip.servlet;

import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.emip.bean.ItemBean;
import com.emip.bean.MenuDefBean;
import com.emip.config.SysConfig;
import com.emip.config.SysParams;
import com.emip.service.ItemService;
import com.emip.service.MenuDefService;


public class InitServlet extends HttpServlet{
	
	private static final long serialVersionUID = 1L;
	private static Logger logger = Logger.getLogger(InitServlet.class);
	
	ApplicationContext appContext;
	
	//Initialize global variables
    public void init() throws ServletException {
    	logger.info("begin initServlet");
    	appContext = 
    		new ClassPathXmlApplicationContext("config/applicationContext.xml");
    	this.initMenu();
    	initSysParams();
    }	
    
    private void initMenu() {
    	MenuDefService menuDefService = 
    			(MenuDefService) appContext.getBean("menuDefService");
    	List<MenuDefBean> menu = menuDefService.getMenuList();
    	SysConfig.getInstance().setMenu(menu);
    }
    
    private void initSysParams() {
    	ItemService itemService = 
    			(ItemService) appContext.getBean("itemService");
    	ItemBean item = itemService.getItemByName("PH");
    	if (item != null) {
    		SysParams.PH = item.getId();
    	}
    }
    
}
